<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <title>
        Java ArrayList |
        
        Darklovy</title>
    <meta name="description" content>
    <meta name="keywords" content>
    <meta name="HandheldFriendly" content="True">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link rel="shortcut icon" href="/">
    <link rel="alternate" href="/atom.xml" title="Darklovy">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="Java 8 官方文档链接 12345678java.lang.Object	java.util.AbstractCollection&amp;lt;E&amp;gt;		java.util.AbstractList&amp;lt;E&amp;gt;			java.util.ArrayList&amp;lt;E&amp;gt;All Implemented Interfaces:	Serializable, Cloneable, Iterabl">
<meta name="keywords" content="Java">
<meta property="og:type" content="article">
<meta property="og:title" content="Java ArrayList">
<meta property="og:url" content="https://www.ttxxly.top/2019/03/02/Java-ArrayList/index.html">
<meta property="og:site_name" content="Darklovy">
<meta property="og:description" content="Java 8 官方文档链接 12345678java.lang.Object	java.util.AbstractCollection&amp;lt;E&amp;gt;		java.util.AbstractList&amp;lt;E&amp;gt;			java.util.ArrayList&amp;lt;E&amp;gt;All Implemented Interfaces:	Serializable, Cloneable, Iterabl">
<meta property="og:locale" content="en">
<meta property="og:updated_time" content="2019-03-02T07:41:44.128Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java ArrayList">
<meta name="twitter:description" content="Java 8 官方文档链接 12345678java.lang.Object	java.util.AbstractCollection&amp;lt;E&amp;gt;		java.util.AbstractList&amp;lt;E&amp;gt;			java.util.ArrayList&amp;lt;E&amp;gt;All Implemented Interfaces:	Serializable, Cloneable, Iterabl">

    <link href="https://fonts.googleapis.com/css?family=Inconsolata|Titillium+Web" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
    <link href="//cdn.bootcss.com/node-waves/0.7.5/waves.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/style.css">
    <script>
        function setLoadingBarProgress(num) {
            document.getElementById('loading-bar').style.width = num + "%";
        }
    </script>
</head>
</html>
<body>
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>


  <script>setLoadingBarProgress(20)</script> 
  <header class="l_header">
	<div class='wrapper'>
		<div class="nav-main container container--flex">
			<a class="logo flat-box" href='/' >
				Darklovy
			</a>
			<div class='menu'>
				<ul class='h-list'>
					
						<li>
							<a class='flat-box nav-Home' href='/'>
								首页
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-archives' href='/archives'>
								归档
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-categories' href='/categories'>
								分类
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-tags' href='/tags'>
								标签
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-algorithm' href='https://book.algorithm.ttxxly.top/'>
								算法
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-python' href='https://book.python.ttxxly.top/'>
								python
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-about' href='/about'>
								关于
							</a>
						</li>
					
				</ul>
				<div class='underline'></div>
			</div>
			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="Search" />
						<span class="icon icon-search"></span>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a href='javascript:void(0)'><span class="icon icon-search flat-box"></span></a></li>
				
				<li class='s-menu'><a href='javascript:void(0)'><span class="icon icon-menu flat-box"></span></a></li>
			</ul>
		</div>
		
		<div class='nav-sub container container--flex'>
			<a class="logo" class="flat-box" href='javascript:void(0)'>
				Word of Forks
			</a>

			<ul class='switcher h-list'>
				<li class='s-comment'><a href='javascript:void(0)'><span class="icon icon-chat_bubble_outline flat-box"></span></a></li>
				<li class='s-top'><a href='javascript:void(0)'><span class="icon icon-arrow_upward flat-box"></span></a></li>
				<li class='s-toc'><a href='javascript:void(0)'><span class="icon icon-format_list_numbered flat-box"></span></a></li>
			</ul>
		</div>
	</div>
</header>
<aside class="menu-phone">
	<nav>
		
			<a href="/" class="nav-Home nav">
				首页
			</a>
		
			<a href="/archives" class="nav-archives nav">
				归档
			</a>
		
			<a href="/categories" class="nav-categories nav">
				分类
			</a>
		
			<a href="/tags" class="nav-tags nav">
				标签
			</a>
		
			<a href="https://book.algorithm.ttxxly.top/" class="nav-algorithm nav">
				算法
			</a>
		
			<a href="https://book.python.ttxxly.top/" class="nav-python nav">
				python
			</a>
		
			<a href="/about" class="nav-about nav">
				关于
			</a>
		
	</nav>
</aside>

    <script>setLoadingBarProgress(40);</script>
  <div class="l_body">
    <div class='container clearfix'>
      <div class='l_main'>
        <article id="post-Java-ArrayList"
  class="post white-box article-type-post"
  itemscope itemprop="blogPost">
	<section class='meta'>
	<h2 class="title">
  	<a href="/2019/03/02/Java-ArrayList/">
    	Java ArrayList
    </a>
  </h2>
	<time>
	  Mar 2, 2019
	</time>
	
	</section>
	
		<section class="toc-wrapper"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#ArrayList-简介"><span class="toc-number">1.</span> <span class="toc-text">ArrayList 简介</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ArrayList-源码剖析"><span class="toc-number">2.</span> <span class="toc-text">ArrayList 源码剖析</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#字段总结"><span class="toc-number">2.1.</span> <span class="toc-text">字段总结</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#构造器"><span class="toc-number">2.2.</span> <span class="toc-text">构造器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#方法摘要"><span class="toc-number">2.3.</span> <span class="toc-text">方法摘要</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#常用API"><span class="toc-number">2.4.</span> <span class="toc-text">常用API</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#小结"><span class="toc-number">2.5.</span> <span class="toc-text">小结</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ArrayList遍历方式"><span class="toc-number">3.</span> <span class="toc-text">ArrayList遍历方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ArrayList-示例"><span class="toc-number">4.</span> <span class="toc-text">ArrayList 示例</span></a></li></ol></section>
	
	<section class="article typo">
  	<div class="article-entry" itemprop="articleBody">
    	<p><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html" target="_blank" rel="noopener">Java 8 官方文档链接</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">java.lang.Object</span><br><span class="line">	java.util.AbstractCollection&lt;E&gt;</span><br><span class="line">		java.util.AbstractList&lt;E&gt;</span><br><span class="line">			java.util.ArrayList&lt;E&gt;</span><br><span class="line">All Implemented Interfaces:</span><br><span class="line">	Serializable, Cloneable, Iterable&lt;E&gt;, Collection&lt;E&gt;, List&lt;E&gt;, RandomAccess</span><br><span class="line">Direct Known Subclasses:</span><br><span class="line">	AttributeList, RoleList, RoleUnresolvedList</span><br></pre></td></tr></table></figure>
<p>[TOC]</p>
<h3 id="ArrayList-简介"><a href="#ArrayList-简介" class="headerlink" title="ArrayList 简介"></a>ArrayList 简介</h3><ul>
<li><code>ArrayList</code> 是基于数组实现的，是一个动态数组，其容量能自动增长。</li>
<li><code>ArrayList</code> 不是线程安全的。多线程环境下请考虑用一下两种方案：<ul>
<li><code>Collections.synchronizedList(List list)</code> 函数返回一个线程安全的ArrayList类</li>
<li>concurrent并发包下的 <code>CopyOnWriteArrayList</code> 类</li>
<li><code>Vector</code> 类 不建议使用。</li>
</ul>
</li>
<li><code>ArrayList</code> 实现了 <code>Serializable</code> 接口，因此支持序列化，能够通过序列化传输。</li>
<li><code>ArrayList</code> 实现了 <code>Cloneable</code> 接口，即覆盖了函数clone()，能被克隆。</li>
<li><code>ArrayList</code> 实现了 <code>RandmoAccess</code>接口，即提供了随机访问功能。<br>  RandmoAccess是java中用来被List实现，为List提供快速访问功能的。<br>  在ArrayList中，我们即可以通过元素的序号快速获取元素对象.</li>
</ul>
<h3 id="ArrayList-源码剖析"><a href="#ArrayList-源码剖析" class="headerlink" title="ArrayList 源码剖析"></a>ArrayList 源码剖析</h3><p>源码分析基于 JDK 1.8.0_201 版本。</p>
<p><a href="https://paste.ubuntu.com/p/DNqhNPpvr4/" target="_blank" rel="noopener">https://paste.ubuntu.com/p/DNqhNPpvr4/</a></p>
<h4 id="字段总结"><a href="#字段总结" class="headerlink" title="字段总结"></a>字段总结</h4><p>从类 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/AbstractList.html" target="_blank" rel="noopener">java.util.AbstractList</a> 继承而来的字段</p>
<ul>
<li><a href="https://docs.oracle.com/javase/8/docs/api/java/util/AbstractList.html#modCount" target="_blank" rel="noopener">modCount</a></li>
</ul>
<h4 id="构造器"><a href="#构造器" class="headerlink" title="构造器"></a>构造器</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">java.util.ArrayList&lt;E&gt;</span><br><span class="line"></span><br><span class="line">ArrayList()</span><br><span class="line">构造一个初始容量为<span class="number">10</span>的空列表。</span><br><span class="line"></span><br><span class="line">ArrayList(Collection&lt;? extends E&gt; c)</span><br><span class="line">按照集合的迭代器返回的顺序构造一个包含指定集合元素的列表。</span><br><span class="line">参数：</span><br><span class="line">c - 要将其元素放入此列表的集合</span><br><span class="line">抛出：</span><br><span class="line">NullPointerException - 如果指定的集合为<span class="keyword">null</span></span><br><span class="line"></span><br><span class="line">ArrayList(<span class="keyword">int</span> initialCapacity)</span><br><span class="line">构造具有指定初始容量的空列表。</span><br><span class="line">参数：</span><br><span class="line">initialCapacity - 列表的初始容量</span><br><span class="line">抛出：</span><br><span class="line">IllegalArgumentException - 如果指定的初始容量为负数</span><br></pre></td></tr></table></figure>
<h4 id="方法摘要"><a href="#方法摘要" class="headerlink" title="方法摘要"></a>方法摘要</h4><table>
<thead>
<tr>
<th>修饰符和类型</th>
<th>方法和描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>boolean</code></td>
<td><code>add(E e)</code>将指定的元素追加到此列表的末尾。</td>
</tr>
<tr>
<td><code>void</code></td>
<td><code>add(int index, E element)</code>将指定元素插入此列表中的指定位置。</td>
</tr>
<tr>
<td><code>boolean</code></td>
<td><code>addAll(Collection&lt;? extends E&gt; c)</code>将指定集合中的所有元素按指定集合的迭代器返回的顺序附加到此列表的末尾。</td>
</tr>
<tr>
<td><code>boolean</code></td>
<td><code>addAll(int index, Collection&lt;? extends E&gt; c)</code>从指定位置开始，将指定集合中的所有元素插入此列表。</td>
</tr>
<tr>
<td><code>void</code></td>
<td><code>clear()</code>从此列表中删除所有元素。</td>
</tr>
<tr>
<td><code>Object</code></td>
<td><code>clone()</code>返回此<code>ArrayList</code>实例的浅表副本。</td>
</tr>
<tr>
<td><code>boolean</code></td>
<td><code>contains(Object o)</code>如果此列表包含指定的元素，则返回<code>true</code>。</td>
</tr>
<tr>
<td><code>void</code></td>
<td><code>ensureCapacity(int minCapacity)</code>如有必要，增加此<code>ArrayList</code>实例的容量，以确保它至少可以容纳由minimum capacity参数指定的元素数。</td>
</tr>
<tr>
<td><code>void</code></td>
<td><code>forEach(Consumer&lt;? super E&gt; action)</code>对每个元素执行给定操作，<code>Iterable</code> 直到处理完所有元素或操作引发异常。</td>
</tr>
<tr>
<td><code>E</code></td>
<td><code>get(int index)</code>返回此列表中指定位置的元素。</td>
</tr>
<tr>
<td><code>int</code></td>
<td><code>indexOf(Object o)</code>返回此列表中第一次出现的指定元素的索引，如果此列表不包含该元素，则返回-1。</td>
</tr>
<tr>
<td><code>boolean</code></td>
<td><code>isEmpty()</code>如果此列表不包含任何元素，则返回<code>true</code>。</td>
</tr>
<tr>
<td><code>Iterator&lt;E&gt;</code></td>
<td><code>iterator()</code>以适当的顺序返回此列表中元素的迭代器。</td>
</tr>
<tr>
<td><code>int</code></td>
<td><code>lastIndexOf(Object o)</code>返回此列表中指定元素最后一次出现的索引，如果此列表不包含该元素，则返回-1。</td>
</tr>
<tr>
<td><code>ListIterator&lt;E&gt;</code></td>
<td><code>listIterator()</code>返回此列表中元素的列表迭代器（按适当顺序）。</td>
</tr>
<tr>
<td><code>ListIterator&lt;E&gt;</code></td>
<td><code>listIterator(int index)</code>从列表中的指定位置开始，返回列表中元素的列表迭代器（按正确顺序）。</td>
</tr>
<tr>
<td><code>E</code></td>
<td><code>remove(int index)</code>删除此列表中指定位置的元素。</td>
</tr>
<tr>
<td><code>boolean</code></td>
<td><code>remove(Object o)</code>从此列表中删除指定元素的第一个匹配项（如果存在）。</td>
</tr>
<tr>
<td><code>boolean</code></td>
<td><code>removeAll(Collection&lt;?&gt; c)</code>从此列表中删除指定集合中包含的所有元素。</td>
</tr>
<tr>
<td><code>boolean</code></td>
<td><code>removeIf(Predicate&lt;? super E&gt; filter)</code>删除此集合中满足给定谓词的所有元素。</td>
</tr>
<tr>
<td><code>protected void</code></td>
<td><code>removeRange(int fromIndex, int toIndex)</code>从此列表中删除索引介于其中<code>fromIndex</code>，包括和<code>toIndex</code>不包含的所有元素 。</td>
</tr>
<tr>
<td><code>void</code></td>
<td><code>replaceAll(UnaryOperator&lt;E&gt; operator)</code>将该列表的每个元素替换为将运算符应用于该元素的结果。</td>
</tr>
<tr>
<td><code>boolean</code></td>
<td><code>retainAll(Collection&lt;?&gt; c)</code>仅保留此列表中包含在指定集合中的元素。</td>
</tr>
<tr>
<td><code>E</code></td>
<td><code>set(int index, E element)</code>用指定的元素替换此列表中指定位置的元素。</td>
</tr>
<tr>
<td><code>int</code></td>
<td><code>size()</code>返回此列表中的元素数。</td>
</tr>
<tr>
<td><code>void</code></td>
<td><code>sort(Comparator&lt;? super E&gt; c)</code>根据指定的顺序对此列表进行排序 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank" rel="noopener"><code>Comparator</code></a>。</td>
</tr>
<tr>
<td><code>Spliterator&lt;E&gt;</code></td>
<td><code>spliterator()</code>在此列表中的元素上创建<em>后期绑定</em> 和<em>失败快速</em> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Spliterator.html" target="_blank" rel="noopener"><code>Spliterator</code></a>。</td>
</tr>
<tr>
<td><code>List&lt;E&gt;</code></td>
<td><code>subList(int fromIndex, int toIndex)</code>返回指定的<code>fromIndex</code>，包含的和<code>toIndex</code>独占的列表部分的视图 。</td>
</tr>
<tr>
<td><code>Object[]</code></td>
<td><code>toArray()</code>以适当的顺序（从第一个元素到最后一个元素）返回包含此列表中所有元素的数组。</td>
</tr>
<tr>
<td><code>&lt;T&gt; T[]</code></td>
<td><code>toArray(T[] a)</code>以适当的顺序返回包含此列表中所有元素的数组（从第一个元素到最后一个元素）; 返回数组的运行时类型是指定数组的运行时类型。</td>
</tr>
<tr>
<td><code>void</code></td>
<td><code>trimToSize()</code>将此<code>ArrayList</code>实例的容量调整为列表的当前大小。</td>
</tr>
</tbody>
</table>
<h4 id="常用API"><a href="#常用API" class="headerlink" title="常用API"></a>常用API</h4><h4 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h4><h3 id="ArrayList遍历方式"><a href="#ArrayList遍历方式" class="headerlink" title="ArrayList遍历方式"></a>ArrayList遍历方式</h3><ol>
<li>通过<strong>迭代器遍历</strong>。即通过Iterator去遍历。</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Integer value = <span class="keyword">null</span>;</span><br><span class="line">Iterator iter = list.iterator();</span><br><span class="line"><span class="keyword">while</span> (iter.hasNext()) &#123;</span><br><span class="line">    value = (Integer)iter.next();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="2">
<li><strong>随机访问，通过索引值去遍历</strong><br>由于ArrayList实现了RandomAccess接口，它支持通过索引值去随机访问元素。</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Integer value = <span class="keyword">null</span>;</span><br><span class="line"><span class="keyword">int</span> size = list.size();</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;size; i++) &#123;</span><br><span class="line">    value = (Integer)list.get(i);        </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="3">
<li><strong>for循环遍历</strong><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Integer value = <span class="keyword">null</span>;</span><br><span class="line"><span class="keyword">for</span> (Integer integ:list) &#123;</span><br><span class="line">    value = integ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="ArrayList-示例"><a href="#ArrayList-示例" class="headerlink" title="ArrayList 示例"></a>ArrayList 示例</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ArrayListTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 创建ArrayList</span></span><br><span class="line">        ArrayList list = <span class="keyword">new</span> ArrayList();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 将“”</span></span><br><span class="line">        list.add(<span class="string">"1"</span>);</span><br><span class="line">        list.add(<span class="string">"2"</span>);</span><br><span class="line">        list.add(<span class="string">"3"</span>);</span><br><span class="line">        list.add(<span class="string">"4"</span>);</span><br><span class="line">        <span class="comment">// 将下面的元素添加到第1个位置</span></span><br><span class="line">        list.add(<span class="number">0</span>, <span class="string">"5"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取第1个元素</span></span><br><span class="line">        System.out.println(<span class="string">"the first element is: "</span>+ list.get(<span class="number">0</span>));</span><br><span class="line">        <span class="comment">// 删除“3”</span></span><br><span class="line">        list.remove(<span class="string">"3"</span>);</span><br><span class="line">        <span class="comment">// 获取ArrayList的大小</span></span><br><span class="line">        System.out.println(<span class="string">"Arraylist size=: "</span>+ list.size());</span><br><span class="line">        <span class="comment">// 判断list中是否包含"3"</span></span><br><span class="line">        System.out.println(<span class="string">"ArrayList contains 3 is: "</span>+ list.contains(<span class="number">3</span>));</span><br><span class="line">        <span class="comment">// 设置第2个元素为10</span></span><br><span class="line">        list.set(<span class="number">1</span>, <span class="string">"10"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过Iterator遍历ArrayList</span></span><br><span class="line">        <span class="keyword">for</span>(Iterator iter = list.iterator(); iter.hasNext(); ) &#123;</span><br><span class="line">            System.out.println(<span class="string">"next is: "</span>+ iter.next());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 将ArrayList转换为数组</span></span><br><span class="line">        String[] arr = (String[])list.toArray(<span class="keyword">new</span> String[<span class="number">0</span>]);</span><br><span class="line">        <span class="keyword">for</span> (String str:arr)</span><br><span class="line">            System.out.println(<span class="string">"str: "</span>+ str);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 清空ArrayList</span></span><br><span class="line">        list.clear();</span><br><span class="line">        <span class="comment">// 判断ArrayList是否为空</span></span><br><span class="line">        System.out.println(<span class="string">"ArrayList is empty: "</span>+ list.isEmpty());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

  	</div>
	  
	  <div class="article-tags tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
		
	
		<div class="art-item-footer">
				
				
					<span class="art-item-right">next：<a href="/2019/03/01/Java-List-集合概述/" rel="next"  title="Java List 集合概述">
						Java List 集合概述
					</a><i class="icon icon-chevron-thin-right"></i></span>
				
		</div>
	
	</section>
	
</article>
<script>
	window.subData = {
		title: 'Java ArrayList',
		tools: true
	}
</script>

      </div>
      <aside class='l_side'>
        
  <section class='m_widget links'>
<div class='header'>Links</div>
<div class='content'>
    <ul class="entry">
    
        <li><a class="flat-box" target="_blank" href="https://iiong.com/">
            <div class='name'>淮城一只猫</div>
        </a></li>
    
    </ul>
</div>
</section>

  <section class='m_widget categories'>
<div class='header'>Categories</div>
<div class='content'>
    
    <ul class="entry">
    
        <li><a class="flat-box" href="/categories/Android基础/"><div class='name'>Android基础</div><div class='badget'>1</div></a></li>
    
    </ul>
    
</div>
</section>

  
<div class="m_widget tagcloud">
    <div class="header">Tags</div>
    <div class='content'>
        <a href="/tags/Android/" style="font-size: 14px; color: #808080">Android</a> <a href="/tags/HTML/" style="font-size: 14px; color: #808080">HTML</a> <a href="/tags/Java/" style="font-size: 20px; color: #000">Java</a> <a href="/tags/Python/" style="font-size: 14px; color: #808080">Python</a>
    </div>
</div>



      </aside>
      <script>setLoadingBarProgress(60);</script>
    </div>
  </div>
  <footer id="footer" class="clearfix">

	<div class="social-wrapper">
  	
      
        <a href="https://github.com/stkevintan" class="social github"
          target="_blank" rel="external">
          <span class="icon icon-github"></span>
        </a>
      
        <a href="https://twitter.com/kevinsfork" class="social twitter"
          target="_blank" rel="external">
          <span class="icon icon-twitter"></span>
        </a>
      
        <a href="/atom.xml" class="social rss"
          target="_blank" rel="external">
          <span class="icon icon-rss"></span>
        </a>
      
    
  </div>
  
  <div>Theme <a href='https://github.com/stkevintan/hexo-theme-material-flow' class="codename">MaterialFlow</a> designed by <a href="http://keyin.me/" target="_blank">Kevin Tan</a>.</div>
  
</footer>


  <script>setLoadingBarProgress(80);</script>
  

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src='//cdn.bootcss.com/node-waves/0.7.5/waves.min.js'></script>
<script src="//cdn.bootcss.com/scrollReveal.js/3.3.2/scrollreveal.min.js"></script>
<script src="/js/jquery.fitvids.js"></script>
<script>
	var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
	var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
	var ALGOLIA_API_KEY = "";
	var ALGOLIA_APP_ID = "";
	var ALGOLIA_INDEX_NAME = "";
  var AZURE_SERVICE_NAME = "";
  var AZURE_INDEX_NAME = "";
  var AZURE_QUERY_KEY = "";
  var BAIDU_API_ID = "";
  var SEARCH_SERVICE = "hexo";
  var ROOT = "/"||"/";
  if(!ROOT.endsWith('/'))ROOT += '/';
</script>
<script src="/js/search.js"></script>
<script src="/js/app.js"></script>


  <script>setLoadingBarProgress(100);</script>
</body>
</html>
